﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;
using SdmxMl.HelpDoc;
using SdmxMl.GenericMetadata;

namespace SdmxMl.Manager
{
    public class EssConcepts
    {
        public ConceptSchemeType CsScheme { get; set; }

        private string infoPath;

        public EssConcepts(string path)
        {
            infoPath = path;
        }

        public void CreateForlang(string lg)
        {
            CsScheme = new ConceptSchemeType();
            CsScheme.Id = "CS_ESS"; CsScheme.AgencyId = "CH1_SGB";
            CsScheme.Name.UpdateDefaultText("SAS variables import");
            Dictionary <string, string> dicoId = new Dictionary<string,string>();

            string line = null;
            using (StreamReader sr = new StreamReader(infoPath + "_" + lg + ".txt", Encoding.Default))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    int ndx = line.IndexOf("=");
                    if (ndx > 0)
                    {
                        //Left part is ID
                        string id = line.Substring(0, ndx).Trim();
                        //Right is text in specified lang
                        string text = line.Substring(ndx + 1).Replace("\"", "");
                        if (text[0] == 'T' && text[1] >= '0' && text[1] <= '9')
                        {
                            int n2 = text.IndexOf(" ");
                            if (n2 > 0)
                                text = text.Substring(n2).Trim();
                        }

                        if (dicoId.ContainsKey(id))
                        {
                            throw new ApplicationException(id + " already defined");
                        }
                        else
                        {
                            dicoId.Add(id, null);
                            ConceptType c = new ConceptType();
                            c.Id = id;
                            c.Name.UpdateForLanguage(lg, text);
                            CsScheme.ConceptList.Add(c);
                        }
                    }
                }
            
            }
        }

        public void UpdateForLang(string lg)
        {
            if (CsScheme == null)
                throw new ApplicationException("ConceptScheme not creared");

            Dictionary<string, string> dicoId = new Dictionary<string, string>();

            string line = null;
            using (StreamReader sr = new StreamReader(infoPath + "_" + lg + ".txt", Encoding.Default))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    int ndx = line.IndexOf("=");
                    if (ndx > 0)
                    {
                        //Left part is ID
                        string id = line.Substring(0, ndx).Trim();
                        //Right is text in specified lang
                        string text = line.Substring(ndx + 1).Replace("\"", "");
                        if (text[0] == 'T' && text[1] >= '0' && text[1] <= '9')
                        {
                            int n2 = text.IndexOf(" ");
                            if (n2 > 0)
                                text = text.Substring(n2).Trim();
                        }

                        if (dicoId.ContainsKey(id))
                        {
                            throw new ApplicationException(id + " already defined. Lang: " + lg);
                        }
                        else
                        {
                            dicoId.Add(id, null);
                            ConceptType c = CsScheme.ConceptList.LocateItem(id) as ConceptType;
                            if (c == null)
                                throw new ApplicationException(id + " Not defined for language " + lg);
                            c.Name.UpdateForLanguage(lg, text);
                        }
                    }
                }

            }
        }

        public void SyncAnnotations(ConceptSchemeType cstAnnot)
        {
            foreach (ConceptType c in CsScheme.ConceptList)
            {
                ISdmxHierarchyListItem item = cstAnnot.ConceptList.LocateItem(c.Id);
                if (item != null)
                {
                    c.AnnotationList = item.AnnotationList;
                    ConceptType cc = item as ConceptType;
                    c.CoreRepresentation.AgencyId = cc.CoreRepresentation.AgencyId;
                    c.CoreRepresentation.Id = cc.CoreRepresentation.Id;
                    c.CoreRepresentation.Version = cc.CoreRepresentation.Version;
                }

            }
        }



    }
}
